package com.bilubi.reggie.reggie.service.Impl;

import com.bilubi.reggie.reggie.bean.*;
import com.bilubi.reggie.reggie.bean.vo.OrderVo;
import com.bilubi.reggie.reggie.mapper.*;
import com.bilubi.reggie.reggie.service.OrderService;
import com.bilubi.reggie.reggie.utils.OrderIdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    AddressBookMapper addressBookMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<OrderVo> selectAllOrderByUserId() {
        Order order = new Order();
        return orderMapper.selectByUserId(order);
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public void submit(Order order) {
        // 根据useId查询购物车，计算金额，封装order对象存入数据库
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByUserId(order.getUserId());
        AddressBook addressBook = addressBookMapper.selectByPrimaryKey(order.getAddressBookId());
        User user = userMapper.selectByPrimaryKey(order.getUserId());
        BigDecimal amount = shoppingCarts.stream().map(ShoppingCart::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        order.setAmount(amount);
        order.setStatus(2);
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAddress(addressBook.getDetail());
        order.setPhone(addressBook.getPhone());
        String numberCode = OrderIdGenerator.generateOrderId();
        order.setNumber(numberCode);
        order.setUserName(user.getName());
        orderMapper.insertSelective(order);
        // 根据查询到的购物车列表，存入order_detail
        Long orderId = order.getId();
        List<OrderDetail> collect = shoppingCarts.stream().map(shoppingCart -> {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart, orderDetail);
            orderDetail.setOrderId(orderId);
            orderDetail.setId(null);
            return orderDetail;
        }).toList();
        orderDetailMapper.insertsSelective(collect);
        // 清空购物车
        shoppingCartMapper.deleteByUserId(new ShoppingCart());
    }

    @Override
    public List<OrderVo> selectAllOrderByUserIdAndTime(Date beginTime, Date endTime, String number) {
        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("number", number);
        return orderMapper.selectByUserIdAndTime(map);
    }

    @Override
    public void updateStatus(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }
}
